package com.aftersale.demo.service.impl;

import com.aftersale.demo.entity.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aftersale.demo.entity.Permission;
import com.aftersale.demo.service.PermissionService;
import com.aftersale.demo.mapper.PermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>
    implements PermissionService{
    @Autowired
    PermissionMapper permissionMapper;
    @Override
    public Result getPermission(Integer pageNum,Integer pageSize) {
        List<Permission> permissionList= permissionMapper.selectList(null);
        Page<Permission> permissionPage=page(new Page<>(pageNum,pageSize));
        return Result.success("权限获取成功",permissionPage);
    }
    @Override
    public Result getPermission() {
        List<Permission> permissionList= permissionMapper.selectList(null);
        return Result.success("权限获取成功",permissionList);
    }

    @Override
    public Result addPermission(Permission permission) {
        int success=permissionMapper.insert(permission);
        if(success==1){
            return Result.success("添加成功");
        }else{
            return Result.error("401","添加失败");
        }
    }

    @Override
    public Result deletePermission(int id) {
        int num=permissionMapper.deleteById(id);
        if(num==1){
            return Result.success("删除成功");
        }else{
            return Result.error("401","删除失败，数据不存在或已删除");
        }
    }

    @Override
    public Result updatePermission(Permission permission) {
        int num=permissionMapper.updateById(permission);
        if(num==1){
            return Result.success("修改成功");
        }else{
            return Result.error("401","修改失败，数据不存在或已修改");
        }
    }

    @Override
    public Result selectPermission(String perName, Integer pageNum, Integer pageSize) {
        QueryWrapper<Permission> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("per_name",perName);
        Page<Permission> permissionPage=page(new Page<>(pageNum,pageSize),queryWrapper);
        return Result.success(permissionPage);
    }

}




